package com.sunda.spmsweb.ordercontroller;


import com.alibaba.fastjson.JSONObject;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsorder.service.IMaterialSapService;
import com.sunda.spmsweb.util.JWTUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 物料表SAP 前端控制器
 * </p>
 *
 * @author Wayne
 * @since 2020-11-13
 */
@RestController
@RequestMapping("/materialSap")
@Api(tags = "SAP 物料基本信息接口", description = "物料基本信息接口")
public class MaterialSapController {

    @Autowired
    IMaterialSapService iMaterialSapService;

    @GetMapping("/getMaterialSapXM")
    @ApiOperation(value="获取SAP物料信息",notes ="获取SAP物料信息")
    public ResponseResult getMaterialSapXM(@RequestParam String materialNo){
        return iMaterialSapService.getMaterialSapXM(materialNo);
    }

    @RequestMapping("/getMaterialSap")
    @ApiOperation(value = "获取SAP物料目录信息", notes = "1.获取SAP一个物料所属目录信息，实例 materialNo = 210023579，返回所属目录结构信息；\n" +
            "2.如果该物料目录信息为空，则返回该物料详细信息；\n" +
            "3.传入空字符串，则返回所有没有目录的物料详细信息；\n" +
            "4.增加搜索限制 AND SAP.SPMS_MARK = 'X' AND SAP.IS_DELETE != 'X' AND SAP.MATERIAL_STATUS = ' ' \n" +
            "20220225 修改，此接口可按照物料描述模糊搜索，请求参数不变，可接受描述搜索", httpMethod = "POST")
    public ResponseResult getMaterialSap(@RequestParam(defaultValue = "") String materialNo,
                                         @RequestParam(defaultValue = "1") int pageNo,
                                         @RequestParam(defaultValue = "20") int pageSize) {
        try {
            return iMaterialSapService.getMaterialSap(materialNo, pageNo, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    /**
     * 20210318 弃用 - Wayne Wu
     *
     * @RequestMapping("/searchMaterialSap")
     * @ApiOperation(value = "模糊/精确查询物料信息(无库存 含长协)", notes = "模糊/精确查询物料信息，分页,\n" +
     * "可选输入字段，目录编号：categoryId = 100101 \n" +
     * "可选输入物料编号精确查找：materialNoList = 210023454,210036868,130011856\n" +
     * "可选输入 materialZhDesc，materialEnDesc，模糊查询", httpMethod = "POST")
     * public ResponseResult searchMaterialSap(@RequestParam(defaultValue = "1") int pageNo,
     * @RequestParam(defaultValue = "20") int pageSize,
     * @RequestParam(required=false) String categoryId,
     * @RequestParam(required=false) String materialNoList,
     * @RequestParam(required = false) String materialZhDesc,
     * @RequestParam(required=false) String materialEnDesc){
     * try {
     * if (pageNo <= 0 || pageSize <= 0){
     * return ResponseResult.error("参数错误请重试！");
     * }
     * return ResponseResult.success().add("materialSapList", iMaterialSapService.searchMaterialSap(pageNo, pageSize, categoryId, materialNoList, materialZhDesc, materialEnDesc));
     * }catch (Exception e){
     * return ResponseResult.error(e.getMessage());
     * }
     * }
     */

    @RequestMapping("/searchMaterialSapNoContract")
    @ApiOperation(value = "模糊/精确查询物料信息(无库存 无长协)", notes = "模糊/精确查询物料信息，分页\n" +
            "可选输入字段，目录编号：categoryId = 100101\n" +
            "可选输入物料编号精确查找：materialNoList = 130011853,210023454\n" +
            "可选输入 materialZhDesc，materialEnDesc，模糊查询\n" +
            "增加搜索限制 AND SAP.SPMS_MARK = 'X' AND SAP.IS_DELETE != 'X' AND SAP.MATERIAL_STATUS = ' ' ", httpMethod = "POST")
    public ResponseResult searchMaterialSapNoContract(@RequestParam(defaultValue = "1") int pageNo,
                                                      @RequestParam(defaultValue = "20") int pageSize,
                                                      @RequestParam(required = false) String categoryId,
                                                      @RequestParam(required = false) String materialNoList,
                                                      @RequestParam(required = false) String materialZhDesc,
                                                      @RequestParam(required = false) String materialEnDesc,
                                                      @RequestParam(required = false) String productLevelDesc,
                                                      @RequestParam(required = false) String specs,
                                                      @RequestParam(required = false) String brand) {
        try {
            if (pageNo <= 0 || pageSize <= 0) {
                return ResponseResult.error("参数错误请重试！");
            }
            return ResponseResult.success().add("materialSapList", iMaterialSapService.searchMaterialSapNoContract(pageNo, pageSize, categoryId, materialNoList, materialZhDesc, materialEnDesc,productLevelDesc,specs,brand));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error(e.getMessage());
        }
    }

    @RequestMapping("/searchMaterialSapNoContractGlobal")
    @ApiOperation(value = "模糊查询物料信息(无库存 无长协 全局)", notes = "模糊查询物料信息，分页\n" +
            "可选输入物料编号模糊查找：materialNoList = 130011853\n" +
            "可选输入 materialZhDesc，materialEnDesc，模糊查询\n" +
            "增加搜索限制 AND SAP.SPMS_MARK = 'X' AND SAP.IS_DELETE != 'X' AND SAP.MATERIAL_STATUS = ' ' ", httpMethod = "POST")
    public ResponseResult searchMaterialSapNoContractGlobal(@RequestParam(defaultValue = "1") int pageNo,
                                                      @RequestParam(defaultValue = "20") int pageSize,
                                                      @RequestParam(required = false) String keyword) {
        try {
            if (pageNo <= 0 || pageSize <= 0) {
                return ResponseResult.error("参数错误请重试！");
            }
            return ResponseResult.success().add("materialSapList", iMaterialSapService.searchMaterialSapNoContractGlobal(pageNo, pageSize, keyword));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error(e.getMessage());
        }
    }

    @RequestMapping("/getMaterialSapInventory")
    @ApiOperation(value = "模糊/精确查询物料信息(含指定仓库库存 无长协)", notes = "模糊/精确查询物料信息(含库存 无长协)\n" +
            "可选输入字段，目录编号：categoryId = 100101 \n" +
            "工厂代码必输：werks = GH01\n" +
            "仓库代码可选输入，为空则忽略仓库代码字段；示例：whsList = 1001,1002\n" +
            "可选输入物料编号精确查找：materialNoList = 130011853,210023454\n" +
            "可选输入 materialZhDesc，materialEnDesc，模糊查询", httpMethod = "POST")
    public ResponseResult getMaterialSapInventory(@RequestParam(defaultValue = "1") int pageNo,
                                                  @RequestParam(defaultValue = "20") int pageSize,
                                                  @RequestParam(required = true) String werks,
                                                  @RequestParam(required = false) String whsStr,
                                                  @RequestParam(required = false) String categoryId,
                                                  @RequestParam(required = false) String materialNoList,
                                                  @RequestParam(required = false) String materialZhDesc,
                                                  @RequestParam(required = false) String materialEnDesc,
                                                  @RequestParam(required = false) String productLevelDesc,
                                                  @RequestParam(required = false) String specs,
                                                  @RequestParam(required = false) String brand) {
        try {
            if (pageNo <= 0 || pageSize <= 0 || werks.trim().length() == 0) {
                return ResponseResult.error("参数错误请重试！");
            }
            return ResponseResult.success().add("materialSapList", iMaterialSapService.getMaterialSapInventory(pageNo, pageSize, werks, whsStr, categoryId, materialNoList, materialZhDesc, materialEnDesc,productLevelDesc,specs,brand));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error(e.getMessage());
        }
    }

    /** 20210318 弃用 - Wayne Wu
     @RequestMapping("/getMaterialSapInventoryInfo")
     @ApiOperation(value = "模糊/精确查询物料信息(含库存 含长协)", notes = "模糊/精确查询物料信息(含库存 含长协)\n" +
     "可选输入字段，目录编号：categoryId = 100101 \n" +
     "工厂代码必输：werks = GH01\n" +
     "可选输入物料编号精确查找：materialNoList = 130011853,210023454\n" +
     "可选输入 materialZhDesc，materialEnDesc，模糊查询", httpMethod = "POST")
     public ResponseResult getMaterialSapInventoryInfo(@RequestParam(defaultValue = "1") int pageNo,
     @RequestParam(defaultValue = "20") int pageSize,
     @RequestParam(required = true) String werks,
     @RequestParam(required=false) String categoryId,
     @RequestParam(required=false) String materialNoList,
     @RequestParam(required = false) String materialZhDesc,
     @RequestParam(required=false) String materialEnDesc){
     try {
     if (pageNo <= 0 || pageSize <= 0 || werks.trim().length() == 0){
     return ResponseResult.error("参数错误请重试！");
     }
     return ResponseResult.success().add("materialSapList", iMaterialSapService.getMaterialSapInventoryInfo(pageNo, pageSize, werks, categoryId, materialNoList, materialZhDesc, materialEnDesc));
     }catch (Exception e){
     return ResponseResult.error(e.getMessage());
     }
     }
     */

    @GetMapping("/getMaterialDataFromSap")
    @ApiOperation(value="实时从SAP获取物料更新接口",notes ="实时从SAP获取物料更新接口\n" +
            "入参为用户输入日期：dataDate = 2021-05-27")
    public ResponseResult getMaterialDataFromSap(String dataDate){
        String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
        return iMaterialSapService.getMaterialDataFromSap(dataDate, userId);
    }

    @PostMapping("/getMaterialDataFromSapRecord")
    @ApiOperation(value="查询物料获取接口操作记录",notes ="查询物料获取接口操作记录\n" +
            "示例参数：\n" +
            "{\n" +
            "\t\"userId\": \"999947\",\n" +
            "\t\"beginDate\": \"\",\n" +
            "\t\"endDate\": \"\",\n" +
            "\t\"pageNo\": \"1\",\n" +
            "\t\"pageSize\": \"20\"\n" +
            "}")
    public ResponseResult getMaterialDataFromSapRecord(@RequestBody JSONObject doc){
        return iMaterialSapService.getMaterialDataFromSapRecord(doc);
    }

    @GetMapping("/getAllMaterialX")
    @ApiOperation(value="获取SPMS系统全部备件物料信息",notes ="实时从SAP获取物料更新接口\n")
    public ResponseResult getAllMaterialX(){
        //String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
        return iMaterialSapService.getAllMaterialX();
    }

}
